import { getGoodsList, addGoods, editGoods } from '../../../services/good/index';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    visible: false,
    type: '',
    content: '',
    kid: '',
    page: {
      pageNo: 1,
      pageSize: 10,
      classifyId: '',
    },
    pullDownRefreshing: false,
    goodsListLoadStatus: 0,
    // emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadHomePage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  seting(e) {},
  getList() {
    getGoodsList({
      param: {
        ...this.data.page,
      },
    }).then((res) => {
      this.setData({
        dataList: res.data.entities,
      });
    });
  },
  setting(e) {
    const { type, id } = e.target.dataset;
    if (type === 'edit') {
      wx.navigateTo({
        url: `/subpages/pages/add/add?id=${id}`,
      });
    } else {
      switch (type) {
        case 'del': {
          this.setData({
            content: '确认删除商品吗？',
          });
          break;
        }
        case 'hit': {
          this.setData({
            content: '确认上架商品吗？',
          });
          break;
        }
        case 'sold': {
          this.setData({
            content: '确认下架商品吗？',
          });
          break;
        }
      }
      this.setData({
        visible: true,
        type: type,
        kid: id,
      });
    }
  },
  cancelDialog() {
    this.setData({
      visible: false,
      type: '',
      kid: '',
    });
  },
  closeDialog() {
    const { type } = this.data;
    switch (type) {
      case 'del': {
        wx.showToast({
          title: '删除成功',
        });
        break;
      }
      case 'hit': {
        wx.showToast({
          title: '上架成功',
        });
        break;
      }
      case 'sold': {
        wx.showToast({
          title: '上架成功',
        });
        break;
      }
    }
    this.setData({
      visible: false,
      type: '',
      kid: '',
    });
  },
  confirmDialog() {
    const { type } = this.data;
    switch (type) {
      case 'del': {
        this.hitGoods(1);
        break;
      }
      case 'hit': {
        this.hitGoods(4);
        break;
      }
      case 'sold': {
        this.hitGoods(5);
        break;
      }
    }
  },
  hitGoods(type) {
    editGoods({
      kid: this.data.kid,
      opType: type,
    })
      .then((res) => {
        this.closeDialog();
        this.getList();
      })
      .catch((err) => {
        wx.showToast({
          title: '操作失败，请稍后重试',
        });
        this.setData({
          visible: false,
          type: '',
          kid: '',
        });
        this.getList();
      });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  handleClick() {
    wx.navigateTo({
      url: '/subpages/pages/add/add',
    });
  },
  async loadGoodsList(fresh = false) {
    const { startTime, endTime, timeRange, param } = this.data;
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1,
    });
    const { pageSize } = this.goodListPagination;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const { data } = await getGoodsList({
        pageNo: pageIndex,
        pageSize: pageSize,
        startTime,
        endTime,
        timeRange,
        classifyId: '',
      });
      this.setData({
        dataList: fresh ? data.entities : this.data.entities.concat(data.entities),
        goodsListLoadStatus: pageIndex == 1 && data.entities.length < 10 ? 2 : data.entities.length > 1 ? 0 : 2,
      });

      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3,
      });
    }
  },
  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },
  onReTry() {
    this.loadGoodsList();
  },
});
